package org.wu.framework.lazy.database.datasource.proxy.handler;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public abstract class AbstractConnectionHandler implements ConnectionHandler {

    /**
     * Sets this connection's auto-commit mode to the given state.
     * If a connection is in auto-commit mode, then all its SQL
     * statements will be executed and committed as individual
     * transactions.  Otherwise, its SQL statements are grouped into
     * transactions that are terminated by a call to either
     * the method {@code commit} or the method {@code rollback}.
     * By default, new connections are in auto-commit
     * mode.
     * <p>
     * The commit occurs when the statement completes. The time when the statement
     * completes depends on the type of SQL Statement:
     * <ul>
     * <li>For DML statements, such as Insert, Update or Delete, and DDL statements,
     * the statement is complete as soon as it has finished executing.
     * <li>For Select statements, the statement is complete when the associated result
     * set is closed.
     * <li>For {@code CallableStatement} objects or for statements that return
     * multiple results, the statement is complete
     * when all of the associated result sets have been closed, and all update
     * counts and output parameters have been retrieved.
     * </ul>
     * <p>
     * <B>NOTE:</B>  If this method is called during a transaction and the
     * auto-commit mode is changed, the transaction is committed.  If
     * {@code setAutoCommit} is called and the auto-commit mode is
     * not changed, the call is a no-op.
     *
     * @param dataSource
     * @param connection
     * @param autoCommit {@code true} to enable auto-commit mode;
     *                   {@code false} to disable it
     * @throws SQLException if a database access error occurs,
     *                      setAutoCommit(true) is called while participating in a distributed transaction,
     *                      or this method is called on a closed connection
     * @see #getAutoCommit
     */
    @Override
    public void setAutoCommit(DataSource dataSource, Connection connection, boolean autoCommit) throws SQLException {
        connection.setAutoCommit(autoCommit);
    }

    /**
     * Retrieves the current auto-commit mode for this {@code Connection}
     * object.
     *
     * @param dataSource
     * @param connection
     * @return the current state of this {@code Connection} object's
     * auto-commit mode
     * @throws SQLException if a database access error occurs
     *                      or this method is called on a closed connection
     * @see #setAutoCommit
     */
    @Override
    public boolean getAutoCommit(DataSource dataSource, Connection connection) throws SQLException {
        return connection.getAutoCommit();
    }

    /**
     * Makes all changes made since the previous
     * commit/rollback permanent and releases any database locks
     * currently held by this {@code Connection} object.
     * This method should be
     * used only when auto-commit mode has been disabled.
     *
     * @param dataSource
     * @param connection
     * @throws SQLException if a database access error occurs,
     *                      this method is called while participating in a distributed transaction,
     *                      if this method is called on a closed connection or this
     *                      {@code Connection} object is in auto-commit mode
     * @see #setAutoCommit
     */
    @Override
    public void commit(DataSource dataSource, Connection connection) throws SQLException {

        if(getAutoCommit(dataSource,connection)){
            // 异常处理
        }else {
            connection.commit();
        }
    }

    /**
     * Undoes all changes made in the current transaction
     * and releases any database locks currently held
     * by this {@code Connection} object. This method should be
     * used only when auto-commit mode has been disabled.
     *
     * @param dataSource
     * @param connection
     * @throws SQLException if a database access error occurs,
     *                      this method is called while participating in a distributed transaction,
     *                      this method is called on a closed connection or this
     *                      {@code Connection} object is in auto-commit mode
     * @see #setAutoCommit
     */
    @Override
    public void rollback(DataSource dataSource, Connection connection) throws SQLException {
        connection.rollback();
    }
}
